package com.yuntian.service.Impl;

import com.yuntian.Vo.admin.AdminLoginVo;
import com.yuntian.entity.Admin;
import com.yuntian.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class AdminUserDetailsService implements UserDetailsService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Admin admin = adminMapper.getByAdminName(username);
        if(admin == null){
            throw new UsernameNotFoundException("用户不存在"+username);
        }
        return AdminLoginVo.builder()
                .adminId(admin.getAdminId())
                .adminName(admin.getAdminName())
                .adminPass(admin.getAdminPass())
                .role(admin.getRole())
                .build();
    }
}
